<?php
namespace app\portal\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\wechat\controller\User;
use think\Db;
class userupdate extends Command
{
    protected function configure()
    {
        $this->setName('userupdate')->setDescription('update wechat user ');
    }

    protected function execute(Input $input, Output $output)
    {
        ini_set('max_execution_time', 0);
        
        $content = '区纪委监委、区委办公室、区委组织部、区委宣传部、区委统战部、区委政法委、区编委办公室、区直属机关党委、区信访局、区府办公室、区财政局、区招商办、琶洲管委会、区机关事务局、区人大、区政协、区总工会、团区委、区妇联、区侨联';
        $content = explode('、',$content);
        $ids = Db::name('wechat_department')->where('name','in',$content)->order('id')->column('id');
        
        $user = new User();
        $data = $user->batchUserList($ids);

        $i = 0;
        $user_datas = $user_part_datas = $datas = [];
        foreach($data as $k=>$v){
            $user_datas[$i]['user_login'] = $v['name']?:'';
            $user_datas[$i]['mobile'] = $v['mobile']?:'';
            $user_datas[$i]['openid'] = $v['userid']?:'';
            $member = Db::name('member')->field('id')->where('openid',$user_datas[$i]['openid'])->find();
            if($member){
                Db::name('member')->where(['id'=>$member['id']])->update([
                    'mobile'=>$user_datas[$i]['mobile'],
                    'user_login'=>$user_datas[$i]['user_login']
                ]);
            }else{
                Db::name('member')->insert($user_datas[$i]);
            }
            $i++;
        }
        $user_part_datas = [];
        $j = 0;
        foreach($data as $k=>$v){
            foreach($v['department'] as $value){
                $user_part_datas[$j]['user_id'] = $v['userid'];
                $user_part_datas[$j]['department_id'] = $value;
                $j++;
            }
        }

        /*$id = Db::name('wechat_tag')->column('tagid');
        $datatag = $user->batchTagUserList($id);
        $b = 0;
        foreach ($datatag as $key => $value) {
            foreach ($value as $k => $v) {
                $datas[$b]['tagid'] = $key;
                $datas[$b]['userid'] = $v['userid']; 
                $b++;
            }
        }*/

        //Db::execute('truncate table cmf_member');
        Db::execute('truncate table cmf_wechat_department_user ');
        //Db::execute('truncate table cmf_wechat_tag_user');
        $res1 = Db::name('wechat_department_user')->insertAll($user_part_datas);
        //$res2 = Db::name('member')->insertAll($user_datas);
        //$res3 = Db::name('wechat_tag_user')->insertAll($datas);
    }
}